`timescale 1ns / 1ps
//This module matches the sequences below
//--------------------------------------
//      match   words
//      {{M}}'d0: <unmacthed> 
{%- for w in words %}
//      {{M}}'d{{w[1]}}: {{w[0]}}
{%- endfor %}      
//--------------------------------------
module acautomaton(
    input clk,
    input rst,
    input [7:0] c, //ASCII code
  {%- if M==1 %}
    output reg match
  {%- else %}
    output reg [{{M-1}}:0] match
  {%- endif %}
    );

    localparam 
    {%- for i in range(0, N) %}
        STATE_{{i}} = {{N}}'b{{ '0'*(N-i-1)+'1'+'0'*i }}
        {%- if loop.last %};{% else %},{% endif %}
    {%- endfor %}
    
    reg [{{N}}:1] state, state_next;
    
    always @(posedge clk or posedge rst) begin
        if(rst)
            state <= STATE_0;
        else
            state <= state_next;
    end
    
    always @( state or c) begin
        case(state)
          {%- for ss in states %}
            STATE_{{loop.index0}}:
                {% for s in ss -%}
                  {%- if s[0] -%}
                if(c=="{{ s[0] }}")
                    state_next <= STATE_{{ s[1] }};
                else {% else %}
                    state_next <= STATE_{{ s[1] }};
                  {%- endif -%}
                {%- endfor -%}
          {%- endfor %}
            default: state_next <= STATE_0;
        endcase
    end
    
    always @( state ) begin
        case(state)
        {%- for w in words %}
            STATE_{{ w[2] }}: match = {{M}}'d{{w[1]}};
        {%- endfor %}
            default: match = {{M}}'d0;
        endcase
    end
endmodule